import { Injectable } from "@nestjs/common";
import { InjectRepository } from "@nestjs/typeorm";
import { TUser } from "./entities/testTable.entity";
import { FindCondition, Like, Repository } from "typeorm";
import { ReqAddUserDto, ReqProTableDto } from "./dto/req-testTable.dto";
import { PaginatedDto } from "src/common/dto/paginated.dto";

@Injectable()
export class TestTableService {
  constructor(@InjectRepository(TUser) private readonly tUserRepository: Repository<TUser>) {}

  /**
   * @description: 分页查询用户列表
   * @param {ReqProTableDto} reqProTableDto
   * @return {*}
   */
  async getProTableList(reqProTableDto: ReqProTableDto): Promise<PaginatedDto<TUser>> {
    const where: FindCondition<TUser> = {};
    if (reqProTableDto.t_user_name) {
      where.t_user_name = Like(`%${reqProTableDto.t_user_name}%`);
    }
    if (reqProTableDto.t_gender) {
      where.t_gender = reqProTableDto.t_gender;
    }
    if (reqProTableDto.t_status) {
      where.t_status = reqProTableDto.t_status;
    }

    const result = await this.tUserRepository.findAndCount({
      where,
      skip: reqProTableDto.skip,
      take: reqProTableDto.take,
    });
    return {
      list: result[0],
      totalNum: result[1],
      pageNum: reqProTableDto.pageNum,
      pageSize: reqProTableDto.pageSize,
    };
  }

  // 新增/编辑
  async addOrUpdate(reqAddUserDto: ReqAddUserDto) {
    await this.tUserRepository.save(reqAddUserDto);
  }

  // 删除
  async delete(t_user_id: string | number) {
    return this.tUserRepository.delete(t_user_id);
  }
}
